package com.zcxy360.sandtable.controller;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.exception.MyException;
import com.zcxy360.sandtable.service.UserService;

/**
 * 用户接口
 * @author 盛亚辉
 *
 */
@RequestMapping("user")
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 获取用户
	 * @param userId 用户id
	 * @return 用户对象
	 */
	@RequiresAuthentication
	@GetMapping("{userId}")
	public Object getByUserId(@PathVariable("userId") Integer userId) {
		return userService.getByUserId(userId);
	}

	/**
	 * 获取用户集合
	 * @param user 用户字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 用户集合
	 */
	@RequiresRoles(value = { "admin", "manager", "teacher" }, logical = Logical.OR)
	@GetMapping
	public Object list(User user, String key,String orderByClause, 
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return userService.list(user, key, orderByClause,pageNum, pageSize, navigatePages);
	}

	/**
	 * 新增用户
	 * @param user 用户字段
	 * @return 用户对象
	 * @throws Exception 创建失败异常
	 */
	@RequiresRoles(value = { "admin", "manager", "teacher" }, logical = Logical.OR)
	@PostMapping
	public Object post(@Validated User user) throws Exception {
		try {
			return userService.post(user);
		}catch(Exception e) {
			throw (e instanceof MyException)?e:new MyException("用户创建失败");
		}
	}

	/**
	 * 导入用户
	 * @param file 上传文件
	 * @param orgId 机构id
	 * @param role 用户角色
	 * @param courseId 课程id
	 * @return 用户集合
	 */
	@RequiresRoles(value = { "admin", "manager", "teacher" }, logical = Logical.OR)
	@PostMapping("import")
	public Object importUser(MultipartFile file, Integer orgId, @RequestParam("role") String role,Integer courseId) {
		return userService.importUser(file, orgId, role,courseId);
	}

	/**
	 * 更新用户
	 * @param user 用户字段
	 * @return 布尔值
	 */
	@RequiresRoles(value = { "admin", "manager", "teacher" }, logical = Logical.OR)
	@PutMapping
	public Object update(@Validated User user) {
		return userService.update(user);
	}

	/**
	 * 删除用户
	 * @param userIds 用户id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value = { "teacher", "manager", "admin" }, logical = Logical.OR)
	@DeleteMapping("{userIds}")
	public Object delete(@PathVariable("userIds") int[] userIds) {
		return userService.delete(userIds);
	}

	/**
	 * 检查用户名,手机号和身份证号的唯一性
	 * @param username 代指username,phone,idCard
	 * @return 布尔值
	 */
	@GetMapping("checkUnique")
	public Object checkName(@RequestParam("username") String username) {
		return userService.checkUnique(username);
	}

	/**
	 * 用户登录
	 * @param user 用户字段
	 * @param rememberMe 记住我
	 * @return 用户对象
	 */
	@PostMapping("login")
	public Object login(User user, @RequestParam(defaultValue = "false") boolean rememberMe) {
		return userService.login(user, rememberMe);
	}

	/**
	 * 用户登出
	 * @return 布尔值
	 */
	@RequiresAuthentication
	@GetMapping("logout")
	public Object logout() {
		return userService.logout();
	}

	/**
	 * 根据当前会话获取用户
	 * @param session 当前会话
	 * @return 用户对象
	 */
	@RequiresAuthentication
	@GetMapping("getBySession")
	public Object geBySessiont(HttpSession session) {
		return (User) session.getAttribute("user");
	}

	/**
	 * 验证密码
	 * @param password 密码
	 * @return 布尔值
	 */
	@RequiresAuthentication
	@GetMapping("checkPassword")
	public Object checkPassword(@RequestParam("password") String password) {
		return userService.checkPassword(password);
	}
	
	/**
	 * 重置密码
	 * @param phone 手机号
	 * @param password 密码
	 * @return 布尔值
	 */
	@GetMapping("resetPassword")
	public Object resetPassword(String phone,@RequestParam("password") String password) {
		return userService.resetPassword(phone,password);
	}
	
}
